<?php

namespace app\member\controller;

/** 控制器类引入区 **/
use app\common\controller\Smscm;

/** 模型类引入区 **/
use app\common\model\ViperUser as ViperUserM;

/** 核心或第三方类引入区 **/

use app\common\model\ViperUser;
use my\Wx;
use think\Db;
use think\Validate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Login extends Base
{
    public $noNeedLogin = ['*'];

    /**
     *  用户登陆
    **/
    public function toLogin()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $ivt_code  =   !empty($data['yqm']) ? $data['yqm'] : 0;

            /*$wx      =  new Wx('123123','12321','authorization_code');
            $result  =  $wx->getGzhAuthLogin($data['code']);

            if(!$result['status'])
            {
                $this->result($result['msg'],'',204);
            }else{
                $openid = $result['openid'];
            }*/

            $openid   = !empty($data['openid']) ? $data['openid'] : 123456;

            $parent           =   [];
            $p_team           =   '';
            if ($ivt_code){
                $parent = ViperUserM::where('ivt_code',$ivt_code)->find();
                if ($parent){
                    //拼接上级ID
                    if(empty($parent['p_team'])){
                        $p_team   =   $parent['id'];
                    }else{
                        $p_team   =   $parent['p_team'].','.$parent['id'];
                    }
                }
            }

            $userInfo = ViperUserM::where('openid',$openid)->field('id,nickname,avatar,status,phone,login_token')->find();
            $token    = !empty($userInfo['login_token']) ? $userInfo['login_token'] : token_rand(32,2);
            Db::startTrans();
            try{

                if($userInfo) {

                    if($userInfo['status'] != 0)
                    {
                        $this->result('该账号已被禁用','',204);
                    }

                    ViperUserM::where('id',$userInfo['id'])->update([
                        'login_token'   =>  $token,
                        'nickname'      =>  $data['nickname'],
                        'avatar'        =>  $data['avatar'],
                    ]);

                } else {

                    //存入用户信息
                    $user_id = ViperUserM::insertGetId([
                        'openid'       =>   $openid,
                        'nickname'     =>   $data['nickname'],
                        'avatar'       =>   $data['avatar'],
                        'p_id'         =>  !empty($parent['id']) ? $parent['id'] : 0,
                        'p_team'       =>  $p_team,
                        'ivt_code'     =>  craete_invite(8,2),
                        'safeword'     =>   time(),
                        'login_token'  =>   $token,
                        'create_time'  =>   time(),
                    ]);

                    $userInfo = ViperUserM::where('id',$user_id)->field('id,nickname,avatar,status,phone')->find();
                }

                Db::commit();
                $re = true;
            }catch (\Exception $e){
                //p($e->getMessage());
                Db::rollback();
                $re = false;
            }

            $is_bind_phone = !empty($userInfo['phone']) ? 1 : 0;
            if($re)
            {
                $this->result('登录成功',['token'=>$token,'is_bind_phone'=>$is_bind_phone],200);
            }else{
                $this->result('登录失败','',204);
            }
        } else {
            $this->result('非法请求', '', 204);
        }

    }


    /**
     *  绑定手机号
    **/
    public function bindPhone()
    {
        $user_id = $this->getUserId();
        $phone   = $this->request->post('phone');
        $code    = $this->request->post('code');
        if(empty($user_id)) $this->result('请登陆','',203);
        if(empty($phone))   $this->result('请填写手机号','',204);
        if(empty($code))    $this->result('请填写验证码','',204);

        //判断验证码是否正确

        $res = ViperUser::where('id',$user_id)->update(['phone'=>$phone]);
        if($res)
        {
            $this->result('绑定成功','',200);
        }else{
            $this->result('绑定失败','',204);
        }
    }


}
